﻿#region Copyrights Notice
//©2010 Microsoft Corporation. All rights reserved.
#endregion

#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
#endregion

namespace CISFAppDeployer
{
    public class Logger
    {
        private Logger()
        {

        }

        public static void WriteLine(string text, string msgType)
        {
            try
            {
                //writing empty line for print UI
                Console.WriteLine("");

                //Get the foreground color to print
                Console.ForegroundColor = getForeGroundColor(msgType);
                
                //print error
                Console.WriteLine(text);
                
                //reset the color
                Console.ResetColor();

                //calling logger now
                WriteToLog(text);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Function to get the ForeGroundColor to print to console
        /// </summary>
        /// <param name="msgType">Type of the message being printed</param>
        /// <returns>void</returns>
        private static ConsoleColor getForeGroundColor(string msgType)
        {
            try
            {
                if (string.IsNullOrEmpty(msgType))
                {
                    return ConsoleColor.Gray;
                }
                else if(msgType == "Error")
                    return ConsoleColor.Red;
                else if (msgType == "Warning")
                    return ConsoleColor.Yellow;
                else if (msgType == "Information")
                    return ConsoleColor.White;
                else
                    return ConsoleColor.Gray;
            }
            catch 
            {
                throw;
            }
        }

        /// <summary>
        /// Function to write the text to the LOG file.
        /// </summary>
        /// <param name="text">Text to be written to the LOG File</param>
        /// <returns>void</returns>
        private static void WriteToLog(string text)
        {
            try
            {
                if (!string.IsNullOrEmpty(text))
                {
                    //log to file now
                    string FilePath = "AppDeployer.log";
                    FileStream fStream = new FileStream(FilePath, FileMode.Append, FileAccess.Write);
                    BufferedStream bfs = new BufferedStream(fStream);
                    StreamWriter sWriter = new StreamWriter(bfs);
                    sWriter.WriteLine(text);
                    sWriter.Close();
                }
            }
            catch 
            {
                throw;
            }
        }

    }
}
